package cn.anc.aonicardv.module.adpter.album;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.util.List;

import androidx.viewpager.widget.PagerAdapter;
import cn.anc.aonicardv.GlideApp;
import cn.anc.aonicardv.R;
import cn.anc.aonicardv.bean.PhotoBean;
import cn.anc.aonicardv.param.ParamManager;
import cn.anc.aonicardv.util.LogUtil;
import uk.co.senab.photoview.PhotoView;
import uk.co.senab.photoview.PhotoViewAttacher;


/**
 * function ： 图片浏览页面数据适配器
 * <p/>
 */
public class ImageBrowserPageAdapter extends PagerAdapter {
    private static final String TAG = ImageBrowserPageAdapter.class.getSimpleName();
    private Activity mActivity;
    private List<PhotoBean> mUrlList;
    private ViewGroup mContainer;
    private boolean isLocal = false;

    public ImageBrowserPageAdapter(Activity context, List<PhotoBean> urlList,boolean islocal) {
        this.mActivity = context;
        this.mUrlList = urlList;
        this.isLocal = islocal;
    }

    @Override
    public int getCount() {
        return mUrlList.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        mContainer = container;
        String imgUrl = mUrlList.get(position).getPath();
        PhotoView photoView = new PhotoView(mActivity);
        photoView.setScaleType(ImageView.ScaleType.FIT_XY);
        photoView.setOnPhotoTapListener(mOnPhotoTapListener);
        if (isLocal){
            imgUrl = ParamManager.CarServerParam.http_base_url + imgUrl;
        }
        LogUtil.e("llcTest0715", "-------instantiateItem---imgUrl:" +imgUrl);
        GlideApp.with(mActivity)
                .load(imgUrl)//资源路径
                .placeholder(R.mipmap.image_loading)//占位图
                .error(R.mipmap.image_loading)//加载失败
                .into(photoView);
        container.addView(photoView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        return photoView;
    }

    private PhotoViewAttacher.OnPhotoTapListener mOnPhotoTapListener = new PhotoViewAttacher.OnPhotoTapListener() {
        @Override
        public void onPhotoTap(View view, float x, float y) {
            mActivity.finish();
        }
    };

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }
}
